package com.jsy.learnnetty.echo;

import io.netty.buffer.ByteBuf;
import io.netty.channel.Channel;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;

import java.nio.charset.StandardCharsets;

/**
 * @author: SongyangJi
 * @description:
 * @since: 2021/10/14
 */


public class EchoClientHandler extends SimpleChannelInboundHandler<ByteBuf> {

    Channel channel;

    @Override
    public void channelActive(ChannelHandlerContext ctx) {
        channel = ctx.channel();
        System.out.println("channel active...");
    }

    @Override
    protected void channelRead0(ChannelHandlerContext ctx, ByteBuf msg) {
        System.out.println("client receive echo from server ----- " + msg.toString(StandardCharsets.UTF_8));
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
        cause.printStackTrace();
        ctx.close();
    }

    public Channel getChannel() {
        return channel;
    }
}
